<?php
namespace User\Controller;
use User\Controller\UserController;

class IndexController extends UserController
{
	public function index()
	{
		if(IS_POST)
		{
			$wxAccount = M('wxuser');

            $wxAccount->wxname 		 = $_POST['wxname'];
            $wxAccount->wxid   		 = $_POST['wxid'];
            $wxAccount->weixin       = $_POST['weixin'];
            $wxAccount->appid        = $_POST['appid'];
            $wxAccount->appsecret    = $_POST['appsecret'];
			$wxAccount->wechat_type  = $_POST['wechat_type'];

            if( $wxAccount->where(array('id' => $this->wxuser['id']))->save() )
            {
            	\Think\Log::record('user id->'.$this->user['id'].' update wxuser:','WARN');  //需加入内容
            	$this->success('操作成功', '/User/Index/index');
            }
            else
            {
            	$this->error('操作失败', '/User/Index/index');
            }

		}
		$developerUrl = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/api/'.$this->wxuser['token'];
		$this->assign('developerUrl', $developerUrl);
		$this->assign('wxuser', $this->wxuser);
		$this->display('index');
	}

	public function logout()
	{
		session(null);
		session_destroy();
		unset($_SESSION);
		redirect('/Home/Index/index');
	}

	public function userinfo()
	{
		if(IS_POST)
		{
			$user = M('users');
            $user->username = $_POST['username'];
            $user->mp = $_POST['mp'];

            if($user->where(array('id' => $this->user['id']))->save())
            {
            	$this->success('操作成功');
            }
            else
            {
                $this->error('操作失败');
            }

		}
		$this->assign('user', $this->user);
		$this->display();
	}

	public function passwordEdit()
	{
		if(IS_POST)
		{

			if(trim($this->user['password']) === md5(trim($_POST['pwd'])))
			{
				//TODO:过滤特殊字符
				if(trim($_POST['new_pwd1']) === trim($_POST['new_pwd2']))
				{
                    //$this->user->password = md5(trim($_POST['new_pwd1']));
                    $user = M('users');
                    $user->password = md5(trim($_POST['new_pwd1']));
                    if($user->where(array('id' => $this->user['id']))->save())
                    {
                    	$this->success('密码修改成功');
                    }
                    else
                    {
                    	$this->error('密码修改失败');
                    }
				}
				else
				{
					$this->error('新密码输入不一致');
				}

			}
			else
			{
				$this->error('原密码输入错误');
			}

		}
	} 


	public function reply()
	{
    	$reply = M('reply')->where(array('token'=>$this->token))->find();

        //消息自动回复（无匹配回复）
        //TODO:assign


        //关键词自动回复
        //TODO:assign

    	//被添加自动回复内容
    	$this->assign('reply',$reply);
        $this->display();
	} 

	public function subscribe()
	{
		if(IS_POST)
		{
			$reply = M('reply');
            // $data['content'] = $_POST['content'];
            // $data['token']   =  $this->token;
            // $data['uid']     =  $this->user['id'];
            $_POST['token']  = $this->token;
            $_POST['uid']    = $this->user['id'];
            
			if( !$reply->create($_POST,3) )
			{
				$this->error($reply->getError());
			}
			else
			{
				$pk = $reply->getPK();
				$method = empty($reply->$pk) ? 'add' : 'save';

				if(!$reply->$method())
				{
                    $this->error('保存失败');
				}
				else
				{
					$this->success('保存成功');
				}

			}
		}
	}

	public function unmatch()
	{
		if(IS_POST)
		{
			//add save action
		}
	}
}
?>